﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HEFramework
{
    public static class HUDSliderSystem
    {
        [EntitySystem]
        public class HUDSliderAwakeSystem : AwakeSystem<HUDSlider>
        {
            protected override void Awake(HUDSlider _self)
            {
            }
        }


        [EntitySystem]
        public class HUDSliderUpdateSystem : UpdateSystem<HUDSlider>
        {
            protected override void Update(HUDSlider _self)
            {
                var gc = _self.GetSceneChild<GameCamera>();
                var camp = _self.GetSceneChild<Camp>();
                if (gc == null || camp == null)
                {
                    return;
                }


                for (int i = 0; i < _self.Elements.Count; i++)
                {
                    _self.Elements[i].IsFree = true;
                    if (_self.Elements[i].IsActive)
                    {
                        _self.Elements[i].IsActive = false;
                    }
                }

                //颜色展示
                using ListObject<Entity> datas = _self.GetScene().GetChildList<Entity>();
                for (int i = 0; i < datas.Count; i++)
                {
                    var ac = datas[i].GetChild<AttributeComponent>();
                    if (ac == null)
                    {
                        continue;
                    }

                    if (ac.IsTeam(camp.MyID))
                    {
                        _self.Show(datas[i], Graphics.COLOR_GREEN, Graphics.COLOR_BLUE);
                    }
                    else if (ac.IsFriend(camp.MyID))
                    {
                        _self.Show(datas[i], Graphics.COLOR_YELLOW, Graphics.COLOR_BLUE);
                    }
                    else if (ac.IsEnemy(camp.MyID))
                    {
                        _self.Show(datas[i], Graphics.COLOR_RED, Graphics.COLOR_BLUE);
                    }
                }
            }
        }

        [EntitySystem]
        public class HUDSliderDestroySystem : DestroySystem<HUDSlider>
        {
            protected override void Destroy(HUDSlider _self)
            {
                var pc = _self.GetSceneChild<PrefabComponent>();
                if (pc == null)
                {
                    return;
                }

                for (int i = 0; i < _self.Elements.Count; i++)
                {
                    pc.Recycle("2d/prefabs/uiitem", "HUDSliderItem", _self.Elements[i].Object.gameObject);
                }

                _self.Elements.Clear();
            }
        }

        public static void Show(this HUDSlider _self, Entity _entity, Color _hpc, Color _mpc, float _w = 0.4f, float _h = 0.4f)
        {
            var ac = _entity.GetChild<AttributeComponent>();
            var tc = _entity.GetChild<TransformComponent>();
            if (ac == null || tc == null)
            {
                return;
            }

            float hpv = ac.X_HP.Value == 0 ? -1 : (float)(ac.HP / ac.X_HP.Value);
            float mpv = ac.X_MP.Value == 0 ? -1 : (float)(ac.MP / ac.X_MP.Value);
            _self.GetSlider(hpv, mpv, _hpc, _mpc, tc.GetTopPos(ac, 5).ToVector(), _w, _h, true);
        }

        public static HUDSlider.HUDSliderElement GetSlider(this HUDSlider _self, float _hpv, float _mpv, Color _hpc, Color _mpc, Vector3 _p, float _w = 1f, float _h = 1f, bool _isActive = false)
        {
            var pc = _self.GetSceneChild<PrefabComponent>();
            if (pc == null)
            {
                return null;
            }

            var hud = (_self.Parent as HUD);
            Transform parent = hud.UIWindowBehaviour.GetTransform();
            HUDSlider.HUDSliderElement element = null;
            var elements = _self.Elements;
            for (int i = 0; i < elements.Count; i++)
            {
                if (elements[i].IsFree)
                {
                    element = elements[i];
                    break;
                }
            }

            if (element == null)
            {
                element = new HUDSlider.HUDSliderElement();
                var obj = pc.GetPrefabSyn("2d/prefabs/uiitem", "HUDSliderItem");
                obj.transform.SetParent(parent.transform);
                obj.transform.localPosition = Vector2.zero;
                element.Object = obj;
                element.HP_Fill = element.Object.GetComponent<TransformBehaviour>().GetTransform("Transform_HP_Fill").GetComponent<Image>();
                element.MP_Fill = element.Object.GetComponent<TransformBehaviour>().GetTransform("Transform_MP_Fill").GetComponent<Image>();
                elements.Add(element);
            }

            element.HP_Fill.fillAmount = _hpv;
            element.HP_Fill.gameObject.SetActive(_hpv != -1);
            element.MP_Fill.fillAmount = _mpv;
            element.MP_Fill.gameObject.SetActive(_mpv != -1);

            element.HP_Fill.color = _hpc;
            element.MP_Fill.color = _mpc;


            element.Pos = hud.SyncItemUIPos(_p);
            element.Object.transform.localPosition = element.Pos;
            element.Object.transform.localScale = new Vector2(_w, _h);

            element.IsFree = false;

            element.IsActive = _isActive;
            return element;
        }
    }
}